import React from "react";
import {Select} from "antd";

const Option = Select.Option;

const witty = {
    formatDate(time) {
        if (!time) return '';
        let date = new Date(time);
        return date.getFullYear + '-' + (date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getMinutes() + '-' + date.getSeconds();
    },
    getOptionList(data) {
        let options = [];
        if (!data) return options;
        data.map((item) => {
            options.push(
                <Option value={item.id} key={item.id}>{item.name}</Option>
            );
        });
        return options;
    },
    /**
     * 判断object是否为空, [true]: 不为空, [false]: 为空
     * @param obj
     * @returns {boolean}
     */
    isValidObj: function (obj) {
        if (obj == null || (obj + "") == "" || (obj + "").length <= 0 || obj == undefined) {
            return false;
        }

        return true;
    },
    /**
     * 验证JSON里面的value是否为空，为空则删除
     * @param json
     */
    validNilParams: function (json = {}) {
        for (let key in json) {
            const val = json[key];

            if (!witty.isValidObj(val) || val == undefined
            ) {
                delete json[key];
            }
        }
    },
    /**
     * 读取图片的Base64
     */
    getImageFileBase64: function (originFileObj, callback) {
        const reader = new FileReader();
        reader.addEventListener("load", () => callback(reader.result));
        reader.readAsDataURL(originFileObj);
    },
}
export default witty;
